window.onload=function(){
	 var totalSecond = $("#remainTime").val();
	   alert(totalSecond);
	   startTimer(totalSecond);
}
function startTimer(totalSecond){
	$('#testTimer').text(formatTime(totalSecond));
	var timer = setInterval(function(){
		totalSecond --;
		if(totalSecond >= 0){
			$('#testTimer').text(formatTime(totalSecond));
		}else{
			alert('考试结束！');
			clearInterval(timer);
			submitTest();
		}
	},1000);
}
function formatTime(totalSecond){
	var hour = Math.floor(totalSecond / 3600);
	var min = Math.floor((totalSecond % 3600) / 60);
	var second = (totalSecond % 60);
	hour = formatTimeNumber(hour);
	min = formatTimeNumber(min);
	second = formatTimeNumber(second);
	return hour + ':' + min + ':' + second;
}
function formatTimeNumber(timeNumber){
	if(timeNumber < 10){
		return "0" + timeNumber;
	}else{
		return timeNumber;
	}
}
function submitTest(){
    var path = $("#path").val();
    var subId = $("#sub_id").val();
	var listLength = $("#listLength").val();	        	
	var result = new Array();
	
	for(var i = 0; i < listLength; i++){
		if(i < (listLength/2)){
			var radio =  $("input[name='"+(i+1)+"']:checked").val();	        			
			if(radio==null||radio ==undefined||radio==""){
				result[i] = "";
			}else{
				result[i] = radio;
			}
		}else{
			var checkbox =[];    
			  $("input[name='"+(i+1)+"']:checked").each(function(){    
				  checkbox.push($(this).val());   
		});	        		        		
		if(checkbox==null||checkbox == undefined||checkbox==""){
			result[i] = "";
		}else{
			result[i] = checkbox;
		}
		
	}
	}	        	
	$.ajax({
		type : 'POST',
		url : path+'/student/checkAnswer',
		data : {
			subId : subId,
			result : result
		},
		traditional: true,
		dataType : "json",
		success : function(data){
			if(data){				
				window.location.href=path+'/student/subjects';
			}
		}
	
   });
}
//function submitTest(){
//	$('#testForm').submit();
//}

$(document).ready(function(){
	 	
	$('#testForm').click(function(){
		if(confirm("确定提交试卷吗？")){
			submitTest();
		}		
		});
});
	   
   
	


